package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.HotSearch;
import cn.com.dcsgo.dto.HotSearchAddDTO;
import cn.com.dcsgo.dto.HotSearchQueryDTO;
import cn.com.dcsgo.dto.HotSearchUpdateDTO;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.HotSearchService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 热搜管理相关接口
 *
 * @author Dcsgo
 * @since 2025/10/27 12:28
 */
@Tag(name = "热搜管理相关接口")
@RestController
@RequestMapping("admin/hotSearch")
public class HotSearchController {
    @Resource
    private HotSearchService hotSearchService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:hotSearch:page')")
    @Operation(summary = "分页获取热搜", description = "热搜数据按排序数降序排序")
    public Result<Page<HotSearch>> getPage(PageDTO pageDTO, HotSearchQueryDTO hotSearchQueryDTO) {
        return Result.success(hotSearchService.getPage(pageDTO, hotSearchQueryDTO));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('admin:hotSearch:save')")
    @Operation(summary = "添加热搜", description = "状态默认为启用")
    public Result<Void> saveHotSearch(@Validated @RequestBody HotSearchAddDTO hotSearchAddDTO) {
        return Result.handle(hotSearchService.save(hotSearchAddDTO));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('admin:hotSearch:update')")
    @Operation(summary = "修改热搜")
    public Result<Void> updateHotSearch(@Validated @RequestBody HotSearchUpdateDTO hotSearchUpdateDTO) {
        return Result.handle(hotSearchService.update(hotSearchUpdateDTO));
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:hotSearch:delete')")
    @Operation(summary = "删除热搜", description = "支持批量删除，参数为热搜ID")
    public Result<Void> deleteHotSearch(@RequestBody List<Long> hotSearchIds) {
        return Result.handle(hotSearchService.removeBatchByIds(hotSearchIds));
    }
}
